package dao.impl;

import dao.VoucherDao;
import entry.Voucher;
import pojo.VoucherAndTwoEnterpriseAndInstituty;
import pojo.VoucherCondition;
import util.DBHelper;
import util.PageHelper;

import java.util.ArrayList;

public class VoucherDaoImpl implements VoucherDao {
    DBHelper dbHelper = new DBHelper();

    /**
     * insertVoucher 如果融资金额不等于凭证信息，新增屏证信息
     *
     * @param voucher 要新增的凭证信息
     * @return boolean 返回新增成功与否
     * @author 谈克思
     */
    @Override
    public boolean insertVoucher(Voucher voucher) {
        String sql = "insert into voucher values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        return dbHelper.update(sql, voucher.getId(), voucher.getNo(), voucher.getAmount()
                , voucher.getEnterprise_id(), voucher.getOpen_enterprise_id(),
                voucher.getInstituty_id(), voucher.getOpen_time(), voucher.getExpiry_time(),
                voucher.getUplink_address(), voucher.getSource(),
                voucher.getStatus(), voucher.getSource_id(), voucher.getPayment_interest_type()) != 0 ? true : false;
    }


    /**
     * updateVoucher 更改凭证信息中id对应的凭证的金融机构id
     *
     * @param voucher 凭证中id属性用于定位一条凭证信息，instituty_id属性为金融机构id要更改为的值
     * @return boolean 返回更改成功与否
     * @author 谈克思
     */
    @Override
    public boolean updateVoucher(Voucher voucher) {
        String sql = "update voucher set instituty_id=? where id=? ";
        return dbHelper.update(sql, voucher.getInstituty_id(), voucher.getId()) != 0 ? true : false;
    }

    /**
     * selectVoucherCount 查询凭证信息个数
     *
     * @param voucherCondition 查询条件
     * @return int 返回有凭证信息和收单开单双方企业信息和金融机构信息的VoucherAndTwoEnterpriseAndInstituty对象集合的元素个数
     * @author 金静远
     */
    @Override
    public int selectVoucherCount(VoucherCondition voucherCondition) {
        StringBuffer sql=new StringBuffer(" select count(voucher.id) as id " +
                " from voucher " +
                " inner join enterprise as E1 on voucher.enterprise_id = E1.id " +
                " inner join enterprise as E2 on voucher.open_enterprise_id = E2.id " +
                " inner join instituty as I on voucher.instituty_id = I.id " +
                " where 1 = 1 ");
        ArrayList list=new ArrayList();
        if(voucherCondition.getNo()!=null){
            sql.append(" and no = ? ");
            list.add(voucherCondition.getNo());
        }
        if(voucherCondition.getEnterprise_id()!=null){
            sql.append(" and enterprise_id = ? ");
            list.add(voucherCondition.getEnterprise_id());
        }
        if(voucherCondition.getOpen_enterprise_id()!=null){
            sql.append(" and open_enterprise_id = ? ");
            list.add(voucherCondition.getOpen_enterprise_id());
        }
        if(voucherCondition.getOpen_time()!=null){
            sql.append(" and open_time = ? ");
            list.add(voucherCondition.getOpen_time());
        }
        if(voucherCondition.getAmount_low()!=null){
            sql.append(" and amount >= ? ");
            list.add(voucherCondition.getAmount_low());
        }
        if(voucherCondition.getAmount_high()!=null){
            sql.append(" and amount <= ? ");
            list.add(voucherCondition.getAmount_high());
        }
        return dbHelper.selectCount(sql.toString(),list.toArray());
    }


    /**
     * selectVoucherList 查询凭证列表
     *
     * @param voucherCondition 查询条件
     * @return ArrayList<VoucherAndTwoEnterpriseAndInstituty> 返回有凭证信息和收单开单双方企业信息和金融机构信息的VoucherAndTwoEnterpriseAndInstituty对象集合
     * @author 闫东杰
     */
    @Override
    public ArrayList<VoucherAndTwoEnterpriseAndInstituty> selectVoucherList(VoucherCondition voucherCondition, PageHelper pageHelper) {
        StringBuffer sql=new StringBuffer("SELECT Voucher.*," +
                "       E1.name as name, " +
                "       E1.social_uniform_code as social_uniform_code, " +
                "       E2.name as open_name, " +
                "       E2.social_uniform_code as open_social_uniform_code, " +
                "       I.name as instituty_name, " +
                "       I.interest as instituty_interest, " +
                "       I.receipt_day as instituty_receipt_day " +
                "FROM voucher " +
                "INNER JOIN enterprise AS E1 ON Voucher.enterprise_id = E1.id " +
                "INNER JOIN enterprise AS E2 ON Voucher.open_enterprise_id = E2.id " +
                "INNER JOIN instituty AS I ON Voucher.instituty_id = I.id " +
                "where 1 = 1 ");
        ArrayList list=new ArrayList();
        if(voucherCondition.getNo()!=null){
            sql.append(" and no = ? ");
            list.add(voucherCondition.getNo());
        }
        if(voucherCondition.getEnterprise_id()!=null){
            sql.append(" and enterprise_id = ? ");
            list.add(voucherCondition.getEnterprise_id());
        }
        if(voucherCondition.getOpen_enterprise_id()!=null){
            sql.append(" and open_enterprise_id = ? ");
            list.add(voucherCondition.getOpen_enterprise_id());
        }
        if(voucherCondition.getOpen_time()!=null){
            sql.append(" and open_time = ? ");
            list.add(voucherCondition.getOpen_time());
        }
        if(voucherCondition.getAmount_low()!=null){
            sql.append(" and amount >= ? ");
            list.add(voucherCondition.getAmount_low());
        }
        if(voucherCondition.getAmount_high()!=null){
            sql.append(" and amount <= ? ");
            list.add(voucherCondition.getAmount_high());
        }
        sql.append(" limit ?, ? ");
        int startIndex = (pageHelper.getWhichPage() - 1) * pageHelper.getPerPage();
        list.add(startIndex);
        list.add(pageHelper.getPerPage());
        return dbHelper.select(sql.toString(), VoucherAndTwoEnterpriseAndInstituty.class, list.toArray());
    }

    /**
     * selectVoucherDetail 根据id查找融资详情信息
     *
     * @param id Voucher凭证的id
     * @return VoucherAndTwoEnterpriseAndInstituty 返回有凭证信息和收单开单双方企业信息和金融机构信息的VoucherAndTwoEnterpriseAndInstituty对象
     * @author 闫东杰
     */
    @Override
    public VoucherAndTwoEnterpriseAndInstituty selectVoucherDetail(int id) {
        String sql = "SELECT Voucher.*," +
                "       E1.name as name," +
                "       E1.social_uniform_code as social_uniform_code," +
                "       E2.name as open_name," +
                "       E2.social_uniform_code as open_social_uniform_code," +
                "       I.name as instituty_name," +
                "       I.interest as instituty_interest," +
                "       I.receipt_day as instituty_receipt_day" +
                " FROM voucher " +
                " INNER JOIN enterprise AS E1 ON Voucher.enterprise_id = E1.id " +
                " INNER JOIN enterprise AS E2 ON Voucher.open_enterprise_id = E2.id " +
                " INNER JOIN instituty AS I ON Voucher.instituty_id = I.id " +
                " where voucher.id = ? ";
        return (VoucherAndTwoEnterpriseAndInstituty)dbHelper.select(sql, VoucherAndTwoEnterpriseAndInstituty.class, id).get(0);
    }
}
